package com.financing.wap.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.financing.wap.common.Pager;
import com.financing.wap.common.SqlName;
import com.financing.wap.dao.IScheduleDao;
import com.financing.wap.dto.ScheduleDTO;
import com.financing.wap.dto.TaskDTO;

@Repository("scheduleDao")
@SuppressWarnings(value={"unchecked", "deprecation"})
public class ScheduleDaoImpl extends BaseDaoImpl<ScheduleDTO, Integer> implements IScheduleDao {

	
	
	@Override
	public Class<ScheduleDTO> getEntityClass() {
		return ScheduleDTO.class;
	}
	
	

	@Override
	public boolean add(TaskDTO task) {
		try{
			sqlMapClientTemplate.insert(SqlName.SCHEDULE_ADD, task);
			return true;
		}catch(Exception e){
			log.error("error when add schedule", e);
			return false;
		}
	}

	@Override
	public List<ScheduleDTO> list(Pager pager, ScheduleDTO schedule) {
		int count = this.listCount(schedule);
		pager.setTotalCount(count);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pager", pager);
		paramMap.put("schedule", schedule);
		return sqlMapClientTemplate.queryForList(SqlName.SCHEDULE_LIST,paramMap);
	}

	@Override
	public List<TaskDTO> dayTaskList(ScheduleDTO schedule) {
		return sqlMapClientTemplate.queryForList(SqlName.SCHEDULE_TASK_LIST, schedule);
	}

	@Override
	public int listCount(ScheduleDTO schedule) {
		Object o  = sqlMapClientTemplate.queryForObject(SqlName.SCHEDULE_LIST_CONUT, schedule);
		return o==null?0:(Integer)o; 
	}

	@Override
	public boolean updateTaskStatus(TaskDTO task) {
		try{
			sqlMapClientTemplate.update(SqlName.SCHEDULE_TASK_UPDATE_STATUS, task);
			return true;
		}catch(Exception e){
			log.error("error update task status", e);
			return false;
		}
	}



	@Override
	public TaskDTO queryTaskById(TaskDTO task) {
		
		return (TaskDTO) sqlMapClientTemplate.queryForObject(SqlName.SCHEDULE_TASK_QUERY_BY_ID, task);
	}

}
